local CommonOHOH = _G.CommonOHOH;
local M = CommonOHOH.class.new(script,script.Parent.AFKBase);
local targetTime = 20
local taskObj= nil
local taskData = nil
function M.new(...)
	return M(...);
end;

function M.ctor(self,...)--构造函数

	M.super.ctor(self,...);
  
end;
function M.dtor(self)--析构函数
    M.super.dtor(self);
end;


function M.SubInit(self)
    --单位s
    if self.IsClient then
        taskData = self.DataStore:GetData("task")
        --如果服务器端添加的数据还未同步，监听更新
        if  not taskData["AFK_AccumOnlineTask"] then
            self.player.UserDataEvent:Connect(function(data)
                if data["task"] then
                    if data["task"]["AFK_AccumOnlineTask"] then
                        taskObj = _G.CommonOHOH.load.AccumOnlineTask().new(self.player,targetTime,"AFK_AccumOnlineTask")
                        self:upd()
                        return
                    end
                end
            end)
        end
    else
        taskData = self.DataStore:GetData("task").__real
        if not taskData["AFK_AccumOnlineTask"] then
            --task无数据，初始化基础任务
            self.ItemCtrl=self.player:GetComponent(_G.ServerOHOH.load.Server_ItemCtrl());
            self.ItemCtrl:AddItem({["task_AFK_AccumOnlineTask"]=0},true,true)
        end
    end
    --创建任务对象
    taskObj = _G.CommonOHOH.load.AccumOnlineTask().new(self.player,targetTime,"AFK_AccumOnlineTask")
    self:upd()
end

function M.upd(self)
    --获得最新数据
    if self.IsClient then
        taskData = self.DataStore:GetData("task")
    else
        taskData = self.DataStore:GetData("task").__real
    end
    --初始化，如果有任务数据且已大于等于目标时间，则更新为已完成
    if taskData["AFK_AccumOnlineTask"] and taskData["AFK_AccumOnlineTask"]>=targetTime then
        self.isDone = true
        self.UpdateEvent:Action()
        return
    end
    --如果没有完成就监听完成事件
    taskObj.CompleteEvent:Connect(function()
       self.isDone = true
       self.UpdateEvent:Action()
    end)
end

function M.RemoveUse(self)
    if not self.RngDataCtrl then
        self.RngDataCtrl=self.player:GetComponent(_G.CommonOHOH.load.Common_RngDataLayer())
    end
    self.RngDataCtrl:AddLucky(-self.value)
end

function M.Use(self)
    if not self.RngDataCtrl then
        self.RngDataCtrl=self.player:GetComponent(_G.CommonOHOH.load.Common_RngDataLayer())
    end
    self.RngDataCtrl:AddLucky(self.value)
end


return M